package com.warren.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.warren.entity.Student;
import com.warren.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author warren
 * @data: 2022/3/28 10:22 AM
 * @decription:
 */
@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @PostMapping("set")
    public void set(@RequestBody Student student) {
        redisTemplate.opsForValue().set("student", student);
    }

    @GetMapping("get/{key}")
    public Student get(@PathVariable("key") String key) {
        return (Student) redisTemplate.opsForValue().get(key);
    }

    @DeleteMapping("delete/{key}")
    public boolean delete(@PathVariable("key") String key) {
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);
    }

    @GetMapping("/string")
    public String stringTest() {
        //存string类型
        redisTemplate.opsForValue().set("str", "hello word");
        return (String) redisTemplate.opsForValue().get("str");
    }

    @GetMapping("/list")
    public List<String> listTest() {
        ListOperations<String, String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list", "Hello");
        listOperations.leftPush("list", "world");
        listOperations.leftPush("list", "java");
        List<String> students= listOperations.range("list", 0, 10);
        return students;
    }

    @GetMapping("/list2")
    public void listTest2() {
        userService.testMybatisPlus();
    }

    @GetMapping("/set")
    public Set<String> setTest() {
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set", "Hello");
        setOperations.add("set", "Hello");
        setOperations.add("set", "world");
        setOperations.add("set","java");
        return setOperations.members("set");
    }

    @GetMapping("/zset")
    public Set<String> zsetTest() {
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset", "world", 4);
        zSetOperations.add("zset", "hello", 1);
        zSetOperations.add("zset", "java", 5);
        return zSetOperations.range("zset", 0, 10);
    }

    /**
     * HashMap key value
     * HashOperations key hashkey value
     * key是每一组数据的ID，hashkey和value是一组完整的HashMap数据，通过key来区分不同的HashMap。
     */
    @GetMapping("/hash")
    public String hashTest() {
        HashMap<String, String> a = new HashMap<>();
        a.put("name", "warren");
        HashMap<String, String> a1 = new HashMap<>();
        a1.put("name1", "warren1");
        HashMap<String, String> a2 = new HashMap<>();
        a2.put("name2", "warren2");
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("hash1", "name", "warren");
        hashOperations.put("hash1", "name1", "warren1");
        String value = (String) hashOperations.get("hash1", "name");
        return value;
    }

    @GetMapping("/connection")
    //获取redis的连接对象
    public String connectionTest() {
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
//        connection.flushAll();
//        connection.flushDb();
        System.out.println(connection);
        Student student = new Student(1, "1", 10.0, new Date());
        //json序列化
        String s = null;
        try {
            s = new ObjectMapper().writeValueAsString(student);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }
}
